package 力扣;

/**
 * @author yyq
 * @create 2022-06-09 9:49
 */
public class leetcode134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int[] nums=new int[gas.length];
        int sum = 0;
        for (int i =0;i<nums.length;i++){
            nums[i] = gas[i] - cost[i];
            sum = nums[i]+sum;
        }
        if(sum>=0){
            int temp=0;
            int startIndex = 0;
            while (true){
                for (int i=startIndex;i<nums.length+startIndex;i++){
                    if(startIndex==nums.length-1) return startIndex;
                    int indexTemp = 0;
                    if(i<nums.length) indexTemp=i;
                    else indexTemp = i-nums.length;
                    temp = temp + nums[indexTemp];
                    if(temp<0){
                        temp=0;
                        startIndex = i+1;
                        break;
                    }
                    if(i==startIndex+nums.length-1&&temp>=0){
                        return startIndex;
                    }
                }

            }

        }
        else return -1;
    }
}
